<template>
	<el-container>
		<el-container>
            <el-header>
				<div class="right-panel">
                    <el-input v-model="search.keywords" placeholder="设备名称" clearable @change="upsearch"></el-input>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
			</el-header>
            <el-header>
                <el-button type="primary" icon="el-icon-plus" @click="add" v-auth="'dianProject.add'">新增点检项目</el-button>
            </el-header>
			<el-main class="nopadding">
                <scTable ref="table" :apiObj="apiObj" stripe remoteSort remoteFilter border>
					<el-table-column type="selection" width="50" align="center"></el-table-column>
                    <el-table-column type="index" label="序号" fixed align="center" width="50"></el-table-column>
					<el-table-column label="设备编号" prop="device_code" align="center"></el-table-column>
					<el-table-column label="设备名称" prop="device_title" align="center"></el-table-column>
					<el-table-column label="设备类别" prop="class_title" align="center"></el-table-column>
					<el-table-column label="点检编号" prop="code" align="center"></el-table-column>
					<el-table-column label="点检名称" prop="title" align="center"></el-table-column>
					<el-table-column label="备注" prop="remark" align="center">
<!--                        <template #default="{row}">-->
<!--                            <el-popover placement="top-start" :width="200" trigger="hover" :content="row.remark">-->
<!--                                <template #reference>-->
<!--                                    <el-button style="padding: 5px 0;" text class="m-2" v-if="row.remark">{{row.remark.substring(0, 8)}}</el-button>-->
<!--                                </template>-->
<!--                            </el-popover>-->
<!--                        </template>-->
                    </el-table-column>
                    <el-table-column label="操作" align="center" width="190">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'dianProject.edit'">编辑</el-button>
								<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
									<template #reference>
										<el-button text type="danger" size="small" v-auth="'dianProject.delete'">删除</el-button>
									</template>
								</el-popconfirm>
                                <el-popconfirm title="确定复制项目吗？" @confirm="table_copy(scope.row)">
                                    <template #reference>
                                        <el-button text type="warning" size="small" v-auth="'dianProject.copy'">复制</el-button>
                                    </template>
                                </el-popconfirm>
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
    <save v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false" draggable></save>
</template>

<script>
import save from './save.vue';

export default {
  components: { save },
	name: "user",
	data() {
		return {
            dialog: {
				save: false
			},
			selection: [],
            apiObj: this.$API.device.deviceDianJian.project.lists,
			search: {
				keywords: null,
			},
            form:{},
		};
	},
	watch: {},
	mounted() { },
	methods: {
        add() {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("add");
			});
		},
        table_edit(row) {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("edit").setData(row);
			});
		},
        async table_copy(row){
            this.form.code = row.code
			this.form.title = row.title
			this.form.device_id = row.device_id
			this.form.check_class_id = row.check_class_id
			this.form.dept_id = row.dept_id
			// this.form.item = row.item
            let a = []
            JSON.parse(row.item).map(item=>{
                a.push({
                    title:item.title,
                    con:item.con,
                    type:Number(item.type),
                    ask:item.ask,
                })
            })
            this.form.item = a
			this.form.remark = row.remark
            var res = await this.$API.device.deviceDianJian.project.add.post(this.form);
            if (res.code == 1) {
                this.$refs.table.refresh()
                this.$message.success("操作成功")
            } else {
                this.$alert(res.message, "提示", { type: 'error' })
            }
        },
        //删除
		async table_del(row, index) {
			var reqData = { id: row.id };
			var res = await this.$API.device.deviceDianJian.project.delete.post(reqData);
			if (res.code == 1) {
				//这里选择刷新整个表格 OR 插入/编辑现有表格数据
				this.$refs.table.tableData.splice(index, 1);
                this.$refs.table.refresh();
				this.$message.success("删除成功");
			} else {
				this.$alert(res.message, "提示", { type: "error" });
			}
		},
        //搜索
		upsearch() {
			this.$refs.table.upData(this.search);
		},
        //本地更新数据
		handleSuccess(data, mode) {
			if (mode == "add") {
				this.$refs.table.refresh();
			} else if (mode == "edit") {
				this.$refs.table.refresh();
			}
		},
	},
};
</script>

<style></style>
